package com.zy.demo;

import com.zy.constant.MqConstant;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

public class AsyncTest {

    @Test
    public void testAsync() throws Exception {

        DefaultMQProducer producer = new DefaultMQProducer("async-producer-group");

        producer.setNamesrvAddr(MqConstant.NAMESRV_ADDR);

        producer.start();

        Message message = new Message("asyncTopic", "one async msg".getBytes());
        // 异步发送消息
        producer.send(message,new SendCallback(){

            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("async send success");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("async send failed");
            }
        });

        System.out.println("do first");

        TimeUnit.SECONDS.sleep(10);

        producer.shutdown();

    }


}
